package com.english.controller;

import com.english.entity.FreeLessonAppointment;
import com.english.service.FreeLessonAppointmentService;
import com.english.utils.R;
import com.english.vo.CancelRequest;
import com.english.vo.UpdateStatusRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/appointment")
public class FreeLessonAppointmentController {

    @Autowired
    private FreeLessonAppointmentService appointmentService;

    // 学生预约 已测
    @PostMapping
    public R<Void> createAppointment(@RequestBody FreeLessonAppointment appointment) {
        appointmentService.createAppointment(appointment);
        return R.success();
    }

    // 取消预约
    @PostMapping("/cancel")
    public R<String> cancelAppointment(@RequestBody CancelRequest req) {
        boolean cancelled = appointmentService.cancelAppointment(req.getAppointmentId(), req.getCancelReason());
        if (cancelled) {
            return R.success("预约取消成功");
        } else {
            return R.success("预约已经是取消状态");
        }
    }

    // 状态流转（确认/完成/缺席）
    @PostMapping("/update-status")
    public R<Void> updateStatus(@RequestBody UpdateStatusRequest req) {
        appointmentService.updateStatus(-1L, req.getStatus(), req.getAdminId());
        return R.success();
    }

    // 查询预约记录 已测
    @GetMapping
    public R<List<FreeLessonAppointment>> queryAppointments(
            @RequestParam(required = false) Long studentId,
            @RequestParam(required = false) String date,
            @RequestParam(required = false) String status) {
        return R.success(appointmentService.queryAppointments(studentId, date, status));
    }

    // 检查学生是否可以预约  每周预约一次
    @GetMapping("/check")
    public R<Boolean> checkCanAppointment(
            @RequestParam Long studentId,
            @RequestParam String date) {
        boolean canAppointment = appointmentService.canStudentAppointment(studentId, date);
        return R.success(canAppointment);
    }
}
